<?php
/**
 * controller file Created by PhpStorm.
 * User: wumengmeng
 * Date: 2020/02/25
 * Time: 15:17
 */

namespace Modules\Shop\Http\Controllers;

use Illuminate\Routing\Controller;
use Illuminate\Support\Facades\Request;
use Modules\Shop\Service\StoreOrderRefundService;
use Excel;

class StoreOrderRefundController extends BaseController
{
    /**
     *  售后 列表
     *
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     * @author wumengmeng <wu_mengmeng@foxmail.com>
     */
    public function order_refund_list()
    {
        $arr_search = (array)Request::all();
        $arr_search['shop_id'] = shop_shop_id();//门店id
        $res = StoreOrderRefundService::get_list($arr_search);
        echo_error($res);
        return view('shop::store_order_refund.index', $res['data']);
    }

    /**
     * 售后详情
     *
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     * @author wumengmeng <wu_mengmeng@foxmail.com>
     */
    public function order_refund_detail()
    {
        $nId = Request::get('id', 0);
        $res = StoreOrderRefundService::get_one((int)$nId);
        echo_error($res);

        $arr_data = [
            'arr_data' => $res['data'],
        ];
        //dd($arr_data);
        return view('shop::store_order_refund.detail', $arr_data);
    }


    /**
     * 售后操作
     */
    public function order_refund_handle()
    {
        $data = (array)Request::all();

        shiwu_start();
        $res = StoreOrderRefundService::order_refund_handle($data);
        shiwu_end($res);

        return $res;
    }

    /**
     * 导出Excel
     */
    public function export()
    {
        $arr_search = (array)Request::all();
        $arr_search['shop_id'] = shop_shop_id();//门店id
        $list = StoreOrderRefundService::get_all_list($arr_search);

        $cellLetter = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'];
        $title = "退单列表" . date('Y-m-d h:i');
        Excel::create($title, function ($excel) use ($list, $cellLetter) {
            foreach ($list as $sheet_name => $item) {
                $cellData = [];
                $cellData[0] = ['退单编号', '退单类型', '退单状态', '退单原因', '商品名称【规格】', '价格', '数量', '退单时间', '退款金额', '处理时间'];
                $num = [];
                foreach ($item as $key => $value) {
                    if (!empty($value['get_order_refund_goods'])) {
                        foreach ($value['get_order_refund_goods'] as $ky => $item) {
                            $product = $item['order_goods']['goods_product'];
                            $goods = $product['goods_info']['name'] . '【' . $product['specs']['name'] . '】';
                            $goods_price = $item['order_goods']['goods_price'];
                            $goods_num = $item['order_goods']['goods_num'];
                            $apply_type = $value['apply_type'] == 3 ? '退货' : '退款';
                            $cellData[] = [
                                $value['refund_no'],       //订单编号
                                $apply_type,               //订单状态
                                $sheet_name,               //订单状态
                                '【' . $value['refund_reason_id']['reason'] . '】' . $value['refund_reason'], //退单原因
                                $goods, $goods_price, $goods_num,
                                $value['created_at'],      //退单时间
                                $value['refund_amount'],   //退款金额
                                $value['deal_at'],         //处理时间
                            ];

                        }
                        $num[$key] = count($value['get_order_refund_goods']);
                    }
                }
                $excel->sheet($sheet_name, function ($sheet) use ($cellData, $num, $cellLetter) {
                    $sheet->rows($cellData);
                    $sheet->getDefaultRowDimension()->setRowHeight(22);//设置行高
                    $sheet->getStyle('A1:J' . count($cellData))->getAlignment()->setVertical('center');//垂直居中
                    for ($i = 0; $i <= count($cellData[0]); $i++) {
                        $cell = $cellLetter[$i];
                        if (in_array($cell, ['A', 'G', 'H'])) {
                            $width = 25;
                        } elseif (in_array($cell, ['B', 'C', 'I'])) {
                            $width = 15;
                        } elseif (in_array($cell, ['E'])) {
                            $width = 50;
                        } else {
                            $width = 20;
                        }
                        $sheet->setWidth($cell, $width);//设置列的宽度

                        if ($cell != 'E' & $cell != 'F' & $cell != 'G') {
                            $start = 2;
                            foreach ($num as $key => $value) {
                                $end = $start + $value - 1;
                                $sheet->mergeCells($cell . $start . ':' . $cell . $end); //合并单元格
                                $start = $end + 1;
                            }
                        }
                    }
                    $sheet->getStyle('A1:J' . count($cellData))->applyFromArray([
                        'alignment' => ['horizontal' => 'center'],//设置居中
                    ]);
                    $sheet->getStyle('D2:E' . count($cellData))->applyFromArray([
                        'alignment' => ['horizontal' => 'left'],
                        'font' => ['size' => 10]//字体设置
                    ]);

                    $sheet->getStyle('A1:T1')->applyFromArray([
                        'font' => ['bold' => true, 'color' => ['rgb' => '0072ff']]//字体设置
                    ]);
                });
            }

            $excel->setactivesheetindex(0);//设置默认打开第一个sheet

        })->export('xls');

    }
}